<template>
  <div class="contentDialog">
    <div class="message_title_dialog">
      <ul style="list-style: none">
        <li class="bli"><div style="width: 300px; height:20px; color: white;font-weight: bolder;font-size: x-large">与【{{ TO }}】单聊中</div></li>
        <li class="dli"><div style="width: 500px; height:20px; color: white;font-weight: bolder;font-size: large">
          <div style="float:left;margin-top: -13px;margin-left: 55%"><img :src="computedImageSrc" width="50" height="50" alt="" /></div>
        </div>
        </li>
        <li class="cli">
          <div style="cursor: pointer; text-align: center;vertical-align: center; width: 180px; height:30px; background-color: rgba(96, 173, 207, 0.9); font-weight: bolder;font-size: large;color: white">
            <span style="margin-top: 20px" @click="offLine">下线</span>
        </div>
        </li>
      </ul>

    </div>
     <div class="message_contentDialog">
      <!-- 消息区 -->
      <div class="message_area" ref="diachatWindow">
        <ul
          class="message_item"
          :style="{
            justifyContent: item.from !== FROM ? 'flex-start' : 'flex-end',
          }"
          v-for="(item, index) in dialogmessageList"
          :key="chat"
        >
<!--           系统消息 -->
          <li  v-if="item.msgType === '20' || item.msgType === '30' ">

            <ul>
              <li style="margin-left:-15px">
                <p class="avator_left"  >
                   <img src="../assets/head/sys.png" style="width: 40px;height: 40px" alt="" />
                </p>
                <div  class="name_left">{{ item.from }}
                  <span style="color: #5a5e66; font-size:small">&nbsp;{{ item.sendTime }}</span></div>

              </li>

              <li style="margin-left:-15px; ">
                <p class="message message_left">  {{ item.content }} </p>
              </li>
            </ul>

          </li>

<!--   聊天对象发来的消息       -->
            <li  v-if=" item.msgType ==='10' && item.to === FROM  && item.channel  === CHAT_ROOM_ID ">

              <ul>
                <li style="margin-left:-15px">
                  <p class="avator_left"  >
                  <img v-if="item.msgType === '10'" :src="computedTaImageSrc" style="width: 40px;height: 40px" alt="" />
                </p>
                  <div  class="name_left">{{ item.from }}
                    <span style="color: #5a5e66; font-size:small">&nbsp;{{ item.sendTime }}</span></div>

                </li>
                <li style="margin-left:-15px; ">
                  <p class="message message_left">  {{ item.content }} </p>
                </li>
              </ul>

            </li>

<!--  我发来的消息         -->

            <li style="margin-top: 10px" v-if="item.msgType === '10' && item.from == FROM  && item.channel ===  CHAT_ROOM_ID" >
              <ul>
                <li style="    float: right;    margin-top: -70px;    margin-right: 20px;">
                  <p class="avator_right" >
                  <img :src="computedImageSrc" style="width: 40px;height: 40px" alt="" />
                </p>
                 </li>
                <li style="margin-top: 45px">
                  <p class="message message_right">
                    {{ item.content }}
                  </p>
                </li>
              </ul>

            </li>

        </ul>
      </div>
    </div>
    <div class="message_input_dialog">
      <el-input
        type="textarea"
        style=" width:90%;height: 65px !important;  color: #fff;   min-height: 105px !important;    /* background-color: transparent; */   border: 0;"
        v-model="dialogtextarea"
        autofocus
        :autosize="{ minRows: 2, maxRows: 5 }"
        resize="none"
        @keyup.enter="keyDown($event)"
      >
      </el-input>
      <div
        style="

            width: 19%;
            display: flex;
            align-items: center;
            justify-contentDialog: flex-end;
            height: 50px;
            margin-top: -14.3%;
    margin-left: 100%;
    background-color: rgba(96, 173, 207, 0.9);
          "
      >
        <img src="../assets/images/send.png" width="40" height="40" style="color: white"></img>
        <el-button @click="sendDialog" >发送</el-button>
      </div>

    </div>
  </div>
</template>

<script>

  import CHAT from '@/utils/Chat'


   export default {

     props: {
       // route object
       TO: {
         type: String,
         required: true
       },
       FROM: {
         type: String,
         default: true
       },
       CHAT_ROOM_ID: {
         type: String,
         default: true
       }
     },

    data() {
      return {
        dialogtextarea: "",
        imgUrl: Math.floor(Math.random() * 17) + 1 ,
        imgTaUrl: Math.ceil(Math.random() * 17) + 1 ,
        onlineUsers:[],
        dialogmessageList: [],

      };
    },
    watch: {

    },

    computed : {

      currentTime() {

        return CHAT.formatDate(new Date());

      },


      computedTaImageSrc() {

        return require(`../assets/head/${this.imgTaUrl}.png`) ;

      },

      computedImageSrc() {

        return require(`../assets/head/${this.imgUrl}.png`) ;

      }

    },
    mounted() {
      // 开始连接 webSocket 进行聊天
     let self = this ;

     // // 校验 from 会话是否有效
      CHAT.init(this.FROM,this.CHAT_ROOM_ID,this.dialogmessageList, this.$refs.diachatWindow) ;

      window.addEventListener("keydown", this.keyDowns);

    },
    methods: {

      /* 回车发送消息 */
      keyDowns(e) {
        if (e.key == "Enter") {
          this.send();
          e.preventDefault(); // 去掉默认的换行
        }
      },
      /* 点击发送消息 */
      sendDialog() {

        if( this.dialogtextarea  == ''){
          return ;
        }

        let self = this ;
        this.$nextTick( function(){

          CHAT.sendMsg(this.TO,this.dialogmessageList,this.dialogtextarea) ;
          this.dialogtextarea = "";
          // self.$refs.diachatWindow.scrollTop = self.$refs.diachatWindow.scrollHeight
        })

       },

      offLine(){
        if(CHAT.SOCKET){
          CHAT.SOCKET.close();
        }

      }

    },

    beforeDestroy() {
      window.addEventListener("keydown", this.keyDowns);
    },
  };
</script>

<style lang="scss" scoped>
  ::v-deep .el-dialogtextarea {
    height: calc(100% - 60px);
  }

  ::v-deep .el-dialogtextarea__inner {
    height: 105px !important;
    color: #fff;
    min-height: 105px !important;
    background-color: transparent;
    border: 0;
  }

  ::v-deep ::-webkit-scrollbar {
    background-color: transparent !important;
  }

  .message_left {
    border-top-right-radius: 5px;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    background-color: #0c1324;
    margin-top: -5px;
  }
  .message_right {
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    background-color: #c05939;
    margin-top: 5px;
    margin-right: 20px;
  }

  .contentDialog {
    border: 2px solid #26c4d1;
    border-radius: 20px;
    background-color: rgba(49, 147, 192, 1);;
    box-shadow: 0 0 10px 1px #26c4d1;
    width: 100%;
    height: 800px;
    display: flex;
    margin: 10px auto auto auto ;

    .message_title_dialog {
      height: 50px;
      line-height: 50px;
      border-radius: 20px;
      color: #082b03;
      text-align: center;
      float: right;
      width: 100%;
      position: absolute;
      margin: auto auto auto auto ;
      background: transparent;
      ul {
        list-style: none;
         .ali {
          margin-top: -10px;
        }

        .bli {
          margin-left: -30px;
        }

        .cli {
          margin-left: 50%;
          line-height: 28px;
        }

      }

      ul li {
        float: left;
        font-weight: 800;
        font-style: normal;
        font-size: 14px;
        display: block; /* 将链接设为块级元素 */
        width: 75px; /* 设置宽度 */
        height: 30px; /* 设置高度 */
        line-height: 22px; /* 设置行高，将行高和高度设置同一个值，可以让单行文本垂直居中 */
        text-align: center; /* 居中对齐文字 */
        color: rgba(0, 0, 0, 0.647058823529412);
        text-decoration: none; /* 去掉下划线 */
      }

    }

    .message_input_dialog {
      // border: 1px solid #3e5a77;
      margin: 5px auto 0 auto;
      width: 80%;
      height: 50px;
      border-radius: 5px;
      box-shadow: 0 0 5px 1px #89d8f0;
      background-color: transparent ;
      position: absolute;
      margin: 83% auto auto auto ;
    }


    .message_contentDialog {
      width: 95%;
      height: 70%;
      border-radius: 5px;
      float: right;
      box-shadow: 0 0 5px 1px #89d8f0;
      background-color: rgba(255, 255, 255, 0.8);
      margin: 6% auto auto auto ;
      position: absolute;
      .message_area {
        width: 100%;
        height: calc(100% - 3px);
        overflow-y: auto;

        .message_item {
          display: flex;
          justify-contentDialog: flex-end;
          margin: 5px 0;
          list-style-type: none;
          ul li {
            list-style-type: none;

          }

          .avator_left {
            width: 40px;
            height: 40px;
            margin-right: 10px;
            margin-left: -50px;
            cursor: pointer;
            border-radius: 10px;

            img {
              display: block;
              width: 100%;
              height: 100%;
             }
          }

          .avator_right {
            width: 40px;
            height: 40px;
            /*margin-right: 106px;*/
            /*margin-left: 10px;*/
            cursor: pointer;
            border-radius: 10px;

            img {
              display: block;
              width: 100%;
              height: 100%;
            }
          }

          .name_left {
            color: #67798e;
            height: 35px;
            width: 250px;
            margin-right: 150px;
            margin-top: -50px;
          }

          .name_right {
            color: #67798e;
            height: 40px;
            width: 50px;
            margin-left: 150px;
            margin-top: -30px;
          }

          .message {
            color: #ffffff;
            padding: 5px;
             border: 1px solid #71acbd;
          }
        }
      }


    }
  }


  ::v-deep .el-button {
    color: #082b03;
    border-radius: 5px;
    border: 1px solid #43a0a8;
    background-image: linear-gradient(to bottom, #15263b, #238298);
    margin-right: 10px;
  }
</style>
